package agents.task;

import java.util.ArrayList;
import java.util.List;

import agents.Ant;
import agents.resource.ResourceAgent;
import domain.Segment;
import domain.TimeInterval;

class StartExplorationAnt extends ExplorationAnt {

	public StartExplorationAnt(TaskAgent origin, Segment destination) {
		super(origin, destination);
	}

	@Override
	public void doStep() {
		ResourceAgent resource = getCurrentResource();
		
		if(resource.hasSegment(getDestination()) && resource.canReach(getOrigin().getCar().getPosition(),getDestination())) { //als auto zich reeds op resource met eindbestemming bevindt
			getTrip().append(new TripStep(null,resource.getFirstExitTimeOf(getOrigin().getCar()) + 1));
			getOrigin().notifyExplorationAntArrival(this);
		} else {
			long firstExitTime = resource.getFirstExitTimeOf(getOrigin().getCar());
			TimeInterval enterNextResourceTime = new TimeInterval(firstExitTime+1);
			List<ResourceAgent> outs = resource.getOutgoingResources();
			List<Ant> children = new ArrayList<Ant>();
			for(ResourceAgent out : outs) {
				EnterResourceAnt child = new EnterResourceAnt(out,enterNextResourceTime,this);
				children.add(child);
			}
			setChildAnts(children);
		}
		terminate();
	}

}
